package com.hippo.core.ui.compose.control

import android.content.Context
import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.res.stringArrayResource
import androidx.lifecycle.ViewModel
import com.alibaba.fastjson.JSONObject
import com.hippo.core.R
import com.hippo.core.core.ext.toJson
import com.hippo.core.state.CleanState
import com.hippo.core.state.getTime
import com.hippo.core.tuya.DpInfo
import com.hippo.core.tuya.DpType
import com.hippo.core.tuya.DpValue
import com.hippo.core.tuya.ThingControl
import com.hippo.core.utils.AppUtil
import java.lang.StringBuilder
import kotlin.collections.set

class CleanVM : ViewModel() {

    var uiState by mutableStateOf(CleanState())



    fun updateState(state: CleanState) {
        uiState = state
        Log.i("sss", "CleanScreen11: ${uiState.toJson()}")
    }


    fun getTimer(context: Context) {
        ThingControl.devId?.let {
            ThingControl.getTimerList(it) { timer->
                Log.i("sss", "getTimer: ${timer.toJson()}")

                if (timer != null) {

                    val timePair = AppUtil.parseTime(timer.time)

                    val modeJson = org.json.JSONObject(timer.value)

                    val week = context.resources.getStringArray(R.array.week)
                    val loopsArray = timer.loops.toCharArray()
                    val sb = StringBuilder()
                    loopsArray.forEachIndexed {index,value->
                        if(value == '1'){
                            sb.append(week[index]).append(" ")
                        }
                    }

                    updateState(
                        uiState.copy(
                            timeId = timer.timerId,
                            hour = timePair?.first ?: 0,
                            minute = timePair?.second ?: 0,
                            loops = timer.loops ?: "0000000",
                            notice = timer.status == 1 ,
                            mode = if (modeJson.getString(DpType.MODE.dpId) == DpValue.Mode.spiral) {
                                2
                            } else {
                                1
                            },
                            repeat = sb.toString(),
                        )
                    )

                }

            }
        }
    }


    fun save(callback: () -> Unit) {

        Log.i("sss", "CleanScreen11: ${uiState.toJson()}")

        updateState(
            uiState.copy(
                isLoading = true
            )
        )

        val dpInfo = if (uiState.mode == 2) {
            DpInfo(DpType.MODE.dpId, DpValue.Mode.spiral)
        } else {
            DpInfo(DpType.MODE.dpId, DpValue.Mode.smart)
        }

        val jsonObject = JSONObject()

        val dpObject = JSONObject()
        dpObject[dpInfo.dpId] = dpInfo.dpValue

        jsonObject["dps"] = dpObject
        jsonObject["time"] = uiState.getTime()

        Log.i("sss", "actions: ${jsonObject.toString()}")

        if(uiState.timeId == null){

            ThingControl.addTimer(
                devId = ThingControl.devId.orEmpty(),
                loops = uiState.loops,
                actions = jsonObject.toString(),
                status = uiState.notice,
            ) {


                if (it) {
                    ThingControl.updateCategoryTimerStatus(ThingControl.devId.orEmpty(), uiState.notice)
                }

                callback()
            }
        }else{

            ThingControl.updateTimer(
                timerId = uiState.timeId.orEmpty(),
                devId = ThingControl.devId.orEmpty(),
                dps = jsonObject.toString(),
                loops = uiState.loops,
                status = uiState.notice,
            ) {

                if (it) {
                    ThingControl.updateCategoryTimerStatus(ThingControl.devId.orEmpty(), uiState.notice)
                }

                callback()
            }
        }

    }


}